/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.server;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bioteam.common.user.User;

import org.apache.commons.io.IOUtils;

public class DSLogServlet extends HttpServlet
{

	public static String logFile="d:/tmp/dslog.txt";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		if (req.getSession(true).getAttribute("user") == null)
		{
			resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
			return;
		}
		User user = (User) req.getSession(true).getAttribute("user");
		if (!user.isAdmin())
		{
			resp.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}
		
		File file=new File(logFile);
		if (!file.exists())
		{
			String content="no log available";
			resp.setContentType("text/plain");
			resp.setContentLength(content.length());
			resp.getWriter().write(content);
			return;
		}
		resp.setContentType("text/plain");
		resp.setContentLength((int)file.length());
		IOUtils.copy(new FileReader(file), resp.getWriter());
		return;
		
		
	}
	

}
